/** * This program peforms multiplication of two matrices entered by the user. * The main method asks for the user to enter two matrices, and then calls * multMatrices() to do the multiplication. The result matrix is then * printed. */ class Product { public static void main (String[] args) { // DECLARE VARIABLES/DATA DICTIONARY int[][] a; // GIVEN: A matrix of integers int[][] b; // GIVEN: A matrix of integers int aRows; // GIVEN: Number of rows in a. int aCols; // GIVEN: Number of columns in a. int bRows; // GIVEN: Number of rows in b. int bCols; // GIVEN: Number of columns in b. int[][] c; // RESULT: Product of a and b // PRINT OUT IDENTIFICATION INFORMATION System.out.println(); System.out.println("ITI 1120 Fall 2006, Lab 10, Example 3"); System.out.println("Name: Diana Inkpen, Student# 123456"); System.out.println(); // READ IN GIVENS System.out.println( "Enter the number of rows in matrix A: " ); aRows = ITI1120.readInt( ); System.out.println( "Enter the number of columns in matrix A: " ); aCols = ITI1120.readInt( ); a = MatrixLib.readIntMatrix( aRows, aCols ); System.out.println( "Enter the number of rows in matrix B: " ); bRows = ITI1120.readInt( ); System.out.println( "Enter the number of columns in matrix B: " ); bCols = ITI1120.readInt( ); b = MatrixLib.readIntMatrix( bRows, bCols ); // BODY OF ALGORITHM c = multMatrices( a, b ); // PRINT OUT RESULTS AND MODIFIEDS System.out.println( ); System.out.println( "The product matrix C is: " ); System.out.println( ); MatrixLib.printMatrix( c ); } // If the 'main' method calls other algorithms, put the method(s) below. /** * Performs matrix multiplication on matrices a and b, returning c. * * Matrices a and b are assumed to be rectangular, where a is m x n, and * b is n x p, for integers m, n, and p. The resulting matrix c will * be m x p. The dimensions are not passed as parameters; instead, * the dimensions are determined from the array lengths. * * GIVENS: a: an m x n matrix containing integers * b: an n x p matrix containing integers */ public static int[][] multMatrices( int[][] a, int[][] b ) { // DECLARE VARIABLES / DATA DICTIONARY int[][] c; // RESULT: the product of matrices a and b int aRows; // INTERMEDIATE: number of rows in a and c int aCols; // INTERMEDIATE: number of columns in a, int bRows; // INTERMEDIATE: number of rows in b, should be equal to aCols int bCols; // INTERMEDIATE: number of columns in b and c int row; // INTERMEDIATE: index for row position in c int col; // INTERMEDIATE: index for column position in c int index; // INTERMEDIATE: summation index int sum; // for summing products. // BODY OF ALGORITHM aRows = a.length; // number of rows in a aCols = a[0].length; // number of cols in a bRows = b.length; // number of rows in b bCols = b[0].length; // number of cols in b // Note: this uses the // assumption that the matrix is a rectangle // so we can can use the length of any row. // Create result matrix. Be careful with dimensions! c = new int[aRows][bCols]; // Loop through each matrix position in c: for ( row = 0; row < aRows; row = row + 1 ) { for ( col = 0; col < bCols; col = col + 1 ) { // To calculate C[row][col], we need a sum. Start // a running total at 0. sum = 0; // Do the summation for ( index = 0; index < aCols; index = index + 1 ) { sum = sum + a[row][index] * b[index][col]; } // At this point, we have the actual c[row][col] value. c[row][col] = sum; // Go to next position in c. } } // RETURN RESULT return c; } }